const query = require('../config/db.config.js')

const newsMod = {
  newslistMod: async () =>{
    let sql = 
    `SELECT  
      *
    FROM 
      newsinfo
    order by createTime desc
    `
    const res = await query(sql)
    return res
  },
  selectwebnewsListMod: async (data) =>{
    const {title , create_name,page ,limit } = data
    let pageCurr = (page -1)* limit
    let occ =''
    let sql = 
    `SELECT  
      *
    FROM 
      newsinfo n
    WHERE
         (n.title LIKE '%${ title|| occ }%' )
       AND (n.create_name LIKE '%${ create_name || occ }%' )
       ORDER BY createTime DESC
       LIMIT ${ pageCurr },${ limit }
       `
    console.log(sql);
    let sql2 = `SELECT COUNT(*) total FROM newsinfo ;`
    const total = await query(sql2)
    const res = await query(sql)
    return {
      total : total[0].total,
      list : res
    }
  },
  createdNewsMod:async(data)=>{
    const {title , create_name, content, img } = data
    let sql = `
    INSERT INTO newsinfo 
      (title , create_name, content, img) 
    VALUES 
      ("${title}","${create_name}",'${content}','${img}')
    `
    await query(sql)
  },
  deletenewsMod:async (data) => {
    const {newsID} = data
    let sql = `DELETE FROM newsinfo  where  newsID = "${newsID}"`
    await query(sql)
  },
  selectNewsDetailMod:async (data) => {
    const {newsID} = data
    let sql = `SELECT * from newsinfo  where  newsID = "${newsID}"`
    const res = await query(sql)
    return res
  },

}

module.exports = newsMod